package com.lntea.lnblog.listener; /**
 * Created by meicai on 2016/6/30.
 */

import com.lntea.lnblog.common.BeanFactory;
import com.lntea.lnblog.config.Configuration;
import lombok.Getter;
import lombok.Setter;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionBindingEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Setter
@Getter
public class ContextLoaderListener implements ServletContextListener,
        HttpSessionListener {

    private String scanPackage;

    // Public constructor is required by servlet spec
    public ContextLoaderListener() {
    }

    // -------------------------------------------------------
    // ServletContextListener implementation
    // -------------------------------------------------------
    public void contextInitialized(ServletContextEvent sce) {
      /* This method is called when the servlet context is
         initialized(when the Web application is deployed). 
         You can initialize servlet context related data here.
      */
        String scanPath = Configuration.getClassPathFileName(scanPackage);
        File scanFile = new File(scanPath);
        if(!scanFile.exists()){
            throw new RuntimeException("找不到目录"+scanPath);
        }
        List<File> scanFileList = searchScanFile(scanFile,".*\\.class$");
        try {
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
            for (File classFile : scanFileList) {
                Class clazz = loader.loadClass(classFile.getAbsolutePath());

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void contextDestroyed(ServletContextEvent sce) {
      /* This method is invoked when the Servlet Context 
         (the Web application) is undeployed or 
         Application Server shuts down.
      */
    }

    // -------------------------------------------------------
    // HttpSessionListener implementation
    // -------------------------------------------------------
    public void sessionCreated(HttpSessionEvent se) {
      /* Session is created. */
    }

    public void sessionDestroyed(HttpSessionEvent se) {
      /* Session is destroyed. */
    }

    /**
     * 根据规则搜索文件
     * @param scanFile
     * @param pattern
     * @return
     */
    private List<File> searchScanFile(File scanFile,String pattern) {
        List<File> scanFileList = new ArrayList<File>();
        if(scanFile.isDirectory()){
            File[] childScanFiles = scanFile.listFiles();
            for (File childScanFile : childScanFiles) {
                scanFileList.addAll(searchScanFile(childScanFile,pattern));
            }
        }else{
            if(pattern != null && scanFile.getName().matches(pattern)){
                scanFileList.add(scanFile);
            }else{
                scanFileList.add(scanFile);
            }
        }
        return scanFileList;
    }
}
